package com.Lubaichen.Wiki.controller;

import com.Lubaichen.Wiki.req.EbookQueryReq;
import com.Lubaichen.Wiki.req.EbookSaveReq;
import com.Lubaichen.Wiki.resp.CommonResp;
import com.Lubaichen.Wiki.resp.EbookQueryResp;
import com.Lubaichen.Wiki.resp.PageResp;
import com.Lubaichen.Wiki.service.EbookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author Lubaichen
 * @date 2022年02月27日 21:24
 */
@RestController
// @Controller : 返回的是页面，前后端分离基本用不到这个注解。
// @ResponseBody : 用来返回字符串或JSON对象，更多的是返回JSON对象。
@RequestMapping("/ebook")
public class EbookController {
    @Autowired
    private EbookService ebookService;

    @GetMapping("/list") //Spring会自动将参数映射到类属性
    public CommonResp ebookList(@Valid EbookQueryReq req) {
        CommonResp<PageResp<EbookQueryResp>> resp = new CommonResp<>();
        PageResp<EbookQueryResp> list = ebookService.list(req);
        resp.setContent(list);
        return resp;
    }

    @PostMapping("/save")
    public CommonResp ebookSave(@Valid @RequestBody EbookSaveReq req){
        CommonResp resp = new CommonResp<>();
        ebookService.save(req);
        return resp;
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp deleteEbookById(@PathVariable Long id) {
        CommonResp resp = new CommonResp<>();
        ebookService.deleteEbookById(id);
        return resp;
    }
}
